package nix.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate封装
 */
public class HibernateUtils {

	private static SessionFactory sessionFactory;

	static {
		Configuration conf = new Configuration().configure();
		sessionFactory = conf.buildSessionFactory();
		// 关闭虚拟机时，释放资源
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			@Override
			public void run() {
				sessionFactory.close();
			}
		}));
	}

	public static Session getSession() {
		return sessionFactory.openSession();
	}

	public static Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
}
